﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class start_learn : System.Web.UI.Page
{

   

    protected RegisteredCourses mobjRegisteredCourses = new RegisteredCourses();

    

    protected Persons currentPerson = new Persons();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["ID"] == null)
        {
            Response.Redirect("./LoginEn.aspx");
        }

        if (Request["rand"] == null)
        {
            if (Request["restart"] == null)
                Response.Redirect("./start_learn.aspx?ID=" + Request["ID"] + "&rand=" + System.Guid.NewGuid().ToString());
            else
                Response.Redirect("./start_learn.aspx?ID=" + Request["ID"] + "&restart="+ Request["restart"] +"&rand=" + System.Guid.NewGuid().ToString());
        }


        checkSecurityAndCustomizePage();


        GlobalVars.addLog(mobjRegisteredCourses.ID, 6);


        if (Request["restart"] != null)
        {
            if (mobjRegisteredCourses.LastSlide == (mobjRegisteredCourses.TotalSlides -1))
                mobjRegisteredCourses.LastSlide = 0;

        }


        if (!Page.IsPostBack)
        {
            Page.Header.Title = mobjRegisteredCourses.objCourses.CourseNameEN;
            Page.DataBind();
        }
    }


    /// <summary>
    /// this function will check the security and customize this page
    /// </summary>
    protected void checkSecurityAndCustomizePage()
    {
        currentPerson = GlobalVars.getCurrentPerson();
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Registered_courses_page))
        {
            Response.Redirect("LoginEN.aspx");
        }


        Courses objCourses = new Courses();
        try { objCourses.ID = long.Parse(Request["ID"].ToString()); }
        catch { Response.Redirect("LoginEN.aspx"); }

       
       


        bool bFound = false;
        foreach (RegisteredCourses objRegisteredCourses in currentPerson.lstRegisteredCourses)
        {
            if (objRegisteredCourses.objCourses.Equals(objCourses))
            {
                bFound = true;

                mobjRegisteredCourses = objRegisteredCourses;
                if (mobjRegisteredCourses.LastSlide == null)
                    mobjRegisteredCourses.LastSlide = 0;


                break;
            }
        }

        if (!bFound)
        {
            Response.Redirect("LoginEN.aspx"); 
        }

    }
}